package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.common.service.RedisService;
import com.coffee.admin.api.platform.query.PlatformPermissionQuery;
import com.coffee.admin.api.platform.result.PlatformPermissionResult;
import com.coffee.admin.api.platform.service.PlatformPermissionService;
import com.coffee.admin.service.platform.bean.PlatformPermission;
import com.coffee.admin.service.platform.dao.PlatformPermissionDao;
import com.coffee.admin.service.platform.dao.PlatformRoleDao;
import com.coffee.admin.service.platform.dao.PlatformRolePermissionDao;
import com.coffee.admin.service.platform.dao.PlatformRoleRelationDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class PlatformPermissionServiceImpl implements PlatformPermissionService {
    @Resource
    private PlatformPermissionDao platformPermissionDao;
    @DubboReference
    private RedisService redisService;
    @Resource
    private PlatformRoleDao platformRoleDao;
    @Resource
    private PlatformRolePermissionDao platformRolePermissionDao;
    @Resource
    private PlatformRoleRelationDao platformRoleRelationDao;


    @Override
    public Page<PlatformPermissionResult> getPager(PlatformPermissionQuery param, PagerInfo pr) {
        Page<PlatformPermissionResult> pager = new Page<PlatformPermissionResult>(pr.getStart(), pr.getPageSize());
        Integer count = platformPermissionDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<PlatformPermission> list = platformPermissionDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, PlatformPermissionResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(PlatformPermissionQuery param) {
        Integer count = platformPermissionDao.count(param);
        return count;
    }

    @Override
    public PlatformPermissionResult getDetail(Integer id) {
        PlatformPermission item = platformPermissionDao.findById(id);
        if (item == null) {
            return null;
        }
        PlatformPermissionResult result = new PlatformPermissionResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<PlatformPermissionResult> getList(PlatformPermissionQuery param, PagerInfo pr) {
        List<PlatformPermission> list = platformPermissionDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, PlatformPermissionResult.class);
    }

    @Override
    public List<PlatformPermissionResult> getList(PlatformPermissionQuery param) {
        List<PlatformPermission> list = platformPermissionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PlatformPermissionResult.class);
    }

    @Override
    public List<PlatformPermissionResult> getList() {
        PlatformPermissionQuery param = new PlatformPermissionQuery();
        List<PlatformPermission> list = platformPermissionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PlatformPermissionResult.class);
    }


    @Override
    public void delete(Integer id) {
        platformPermissionDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(PlatformPermissionResult result) throws BusinessException {
        PlatformPermission item = new PlatformPermission();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            platformPermissionDao.insert(item);
        } else {
            PlatformPermission tmp = platformPermissionDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            platformPermissionDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, PlatformPermissionResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, PlatformPermissionResult> map = new HashMap<Integer, PlatformPermissionResult>();
        List<PlatformPermission> list = platformPermissionDao.findByIds(StringUtil.setToList(idSet));
        List<PlatformPermissionResult> resultList = DTOUtils.convertList(list, PlatformPermissionResult.class);
        for (PlatformPermissionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, PlatformPermissionResult> getItemMap() {
        Map<Integer, PlatformPermissionResult> map = new HashMap<Integer, PlatformPermissionResult>();
        PlatformPermissionQuery param = new PlatformPermissionQuery();
        List<PlatformPermission> list = platformPermissionDao.page(param, 0, Integer.MAX_VALUE);
        List<PlatformPermissionResult> resultList = DTOUtils.convertList(list, PlatformPermissionResult.class);
        for (PlatformPermissionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Set<String> getPermissionSet(Integer platformId) throws BusinessException {
        ValidateBusinessException.assertNonNull(platformId, ExceptionEnum.ERROR_1301);
        Set<String> set = new HashSet<String>();
        List<Integer> roleIdList = platformRoleRelationDao.findRoleIdListByPlatformId(platformId);
        if (roleIdList == null || roleIdList.size() == 0) {
            return set;
        }
        List<String> localPermissionList = platformPermissionDao.getPlatformPermissionListByRoleIdList(roleIdList);
        if (localPermissionList == null || localPermissionList.size() == 0) {
            return set;
        } else {
            set.addAll(localPermissionList);
        }
        return set;
    }

    @Override
    public List<PlatformPermissionResult> getPermissionListByLoginMerchant() {

        PlatformPermissionQuery permissionQuery = new PlatformPermissionQuery();
        List<PlatformPermission> platformPermissions = platformPermissionDao.list(permissionQuery);

        Map<Integer, List<PlatformPermission>> permissionMap = platformPermissions.stream().collect(Collectors.groupingBy(PlatformPermission::getPid));

        List<PlatformPermission> permissions = permissionMap.get(0);
        List<PlatformPermissionResult> adminMenuResults = DTOUtils.convertList(permissions, PlatformPermissionResult.class);
        recursion(permissionMap, adminMenuResults);
        return adminMenuResults;
    }

    public void recursion(Map<Integer, List<PlatformPermission>> permissionMap, List<PlatformPermissionResult> adminMenuResults) {
        for (PlatformPermissionResult adminMenuResult : adminMenuResults) {
            Integer id = adminMenuResult.getId();
            List<PlatformPermission> adminMenus = permissionMap.get(id);
            if (CollectionUtils.isNotEmpty(adminMenus)) {
                List<PlatformPermissionResult> submenuList = DTOUtils.convertList(adminMenus, PlatformPermissionResult.class);
                recursion(permissionMap, submenuList);
                adminMenuResult.setChildList(submenuList);
            }
        }
    }

}
